<template>
  <div class="login" id="login">
    <BgLogin :pagename="pagename"></BgLogin>  
    <transition name="el-fade-in">
      <el-row type="flex" v-show="show" class="row-container">
        <div class="form-container">
          <!-- <div class="bar-left"></div> -->
          <!-- <div class="bar-right"></div>   -->
          <el-form
            :model="ruleForm"
            status-icon
            :rules="rules"
            ref="rule"
            label-width="0px"
            label-position="left"
            class="ruleForm"
          >
            <h3 class="title">User Login</h3>
            <el-form-item label prop="username">
              <el-input
                type="username"
                v-model.trim="ruleForm.username"
                placeholder="username"
                maxlength="100"
                auto-complete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label prop="pass">
              <el-input
                type="password"
                v-model.trim="ruleForm.pass"
                placeholder="password"
                maxlength="30"
                auto-complete="off"
              ></el-input>
            </el-form-item>
            <el-form-item class="t-center">
              <el-button class="login-btn" type="primary" @click="submitForm('rule')">Login</el-button>
              <!-- <el-button @click="resetForm('rule')">重置</el-button> -->
            </el-form-item>
          </el-form>
          <!-- <div class="form-right"></div> -->
        </div>
      </el-row>
    </transition>
  </div>
</template>

<script lang="ts">
import { loginServer } from "@/factories/user";
import { setLocalStroage } from "@/utils/stroage";
import { Component, Provide, Emit, Vue } from "vue-property-decorator";
import { Form } from "element-ui";
import BgLogin from "@c/BgLogin.vue";

@Component({
  components: {
    BgLogin
  }
})
export default class Login extends Vue {
  @Provide() pagename:string = 'login'
  @Provide() validatePass = (rule, value, callback) => {
    if (value === "") {
      callback(new Error("请输入密码"))
    } else {
      callback()
    }
  };

  @Provide() ruleForm: {
    pass: String,
    username: String
  } = {
    pass: "",
    username: ""
  };

  @Provide() rules = {
    username: [
      {
        trigger: "blur",
        min: 5,
        required: true,
        max: 100,
        message: "邮箱长度应在6-100位之间！"
      }
    ],
    pass: [
      {
        validator: this.validatePass,
        required: true,
        min: 6,
        max: 30,
        trigger: "blur"
      }
    ]
  };

  @Provide() show: Boolean = false;

  mounted() {
    (this.show as Boolean) = true;
  }

  // methods
  @Emit()
  submitForm(formName) {
    (this.$refs[formName] as Form).validate(valid => {
      if (valid) {
        let params = {
          username: this.ruleForm.username,
          password: this.ruleForm.pass
        };
        loginServer(params).then(res => {
          if (res.data.success) {
            this.$message({
              message: "恭喜你，登陆成功！",
              type: "success"
            });
            setLocalStroage("token", res.data.token);
            setLocalStroage("isLogin", "true");
            this.$router.push("/admin");
          } else {
            this.$message.error("登陆失败，账户或密码错误！");
          }
        });
      } else {
        this.$message.error("请输入用户名和密码！");
        return false;
      }
    });
  }

  @Emit()
  resetForm(formName) {
    (this.$refs[formName] as Form).resetFields();
  }
}
</script>
 
<style lang='scss'>
@mixin bar {
  display: block;
  position: absolute;
  width: 0px;
  height: 2px;
  background: #7a7a7a;
  z-index: 1;
}
.login {
  height: 100%;
  width: 100%;
  background: #000;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  .form-container {
    position: relative;
    width: 400px;
    display: flex;
    flex-direction: row;
    justify-content: center;
    background: rgba(0,0,0,0.4);
    // border: 1px solid #8a6322;
    // box-shadow: 0px 0px 1px #47909c;
    z-index: 2;
    .bar-left {
      @include bar;
      animation: animate1 2s infinite;
      left: 0;
      top: -2px;
    }
    .bar-right {
      @include bar;
      animation: animate2 2s infinite;
      right: 0px;
      bottom: -2px;
    }
    .ruleForm {
      position: relative;
      padding: 0 20px;
      box-sizing: border-box;
      z-index: 3;
      .t-center {
        text-align: center;
      }
      .el-form-item {
        margin-bottom: 30px;
        .el-input__inner {
          background: none;
          border: 1px solid rgba(255,255,255,0.5);
          color: #ccc;
          &:focus {
            color: #fff;
            transition: all 0.5s;
            transform: scale(1.1);
            border: 1px solid #adff2f;
            background: rgba(0,0,0,0.5);
          }
        }
        .login-btn {
          background: #000;
          width: 100%;
          color: #ccc;
          border: 1px solid rgba(255,255,255,0.5);
          transition: all 0.5s;
          &:hover {
            color: #fff;
            background: rgba(146, 241, 0, 0.5);
          }
        }
      }
    }
    .form-right {
      display: flex;
      flex: 1;
      background: url("../../../assets/images/loading.gif") no-repeat center
        center;
      background-size: cover;
    }
  }

  .title {
    text-align: center;
    color: #999;
    position: relative;
    margin: 22px 0;
    animation: titleAnimate 1s;
  }
  .transition-box {
    margin-bottom: 10px;
    width: 200px;
    height: 100px;
    border-radius: 4px;
    background-color: #409eff;
    text-align: center;
    color: #fff;
    padding: 40px 20px;
    box-sizing: border-box;
    margin-right: 20px;
  }
}
@keyframes animate1 {
  0% {
    left: -3px;
  }
  50% {
    width: 10px;
    left: 50%;
  }
  100% {
    left: 98%;
    border-radius: 50%;
  }
}

@keyframes animate2 {
  0% {
    right: -3px;
  }
  50% {
    width: 40px;
    right: 50%;
  }
  100% {
    right: 98%;
    border-radius: 50%;
  }
}

@keyframes titleAnimate {
  0% {
    top: 300px;
    color: #000;
    font-size: 16px;
  }
  50% {
    top: -50px;
    font-size: 26px;
  }
  100% {
    top: 0px;
    color: #ccc;
    font-size: 16px;
  }
}
</style>
